package com.one.shopping.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.one.shopping.R;
import com.one.shopping.api.OneApi;
import com.one.shopping.entity.Goods;
import com.one.shopping.ui.baseActivity.BaseActivity;
import com.one.shopping.util.CommonUtils;

/**
 * @author 柯尊诚
 * @version 1.0
 * @date 创建时间：2016/1/11 23:31
 */
public class GoodsDetailActivity extends BaseActivity{

    private TextView goodsIdValues;
    private TextView nameValues;
    private ImageView imageValues;
    private TextView stockValues;
    private TextView priceValues;
    private Button updateBtn;
    private Goods goods;

    @Override
    protected int getContentViewId() {
        return R.layout.list_goods_detail;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("商品详情");
        isVisibleAdd(true);

        goodsIdValues = findView(R.id.goods_detail_id_values_tv);
        nameValues = findView(R.id.goods_detail_name_values_tv);
        imageValues = findView(R.id.goods_detail_image_values_iv);
        stockValues = findView(R.id.goods_detail_stock_values_tv);
        priceValues = findView(R.id.goods_detail_price_values_tv);
        updateBtn = findView(R.id.goods_update_btn);

    }

    @Override
    protected void initData() {
        super.initData();

        Intent intent = getIntent();
        goods = (Goods)intent.getSerializableExtra("goods");

        goodsIdValues.setText(goods.getId() + "");
        nameValues.setText(goods.getName());

        stockValues.setText(goods.getStock() + "");
        priceValues.setText(goods.getPrice() + "");

        String imageUrl = OneApi.IMAGE_URL + goods.getImage();
//        ImageLoader.getInstance().displayImage(imageUrl, imageValues, ImageLoaderUtils.getOption());
        CommonUtils.setImageForUrl(imageValues, imageUrl);
    }

    @Override
    protected void initListener() {
        super.initListener();

        addBtn.setOnClickListener(onClickAddBtnListener);

        updateBtn.setOnClickListener(onClickUpdateBtnListener);
    }

    /**
     * 点击添加按钮
     */
    private View.OnClickListener onClickAddBtnListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
            Intent intent = new Intent(GoodsDetailActivity.this, AddGoodsActivity.class);
            startActivity(intent);
        }
    };

    /**
     * 点击更新按钮
     */
    private View.OnClickListener onClickUpdateBtnListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
            Intent intent = new Intent(GoodsDetailActivity.this, AddGoodsActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("goods", goods);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    };
}
